import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { ActivatedRoute } from '@angular/router';
//Base components
import { SubIndustryModel } from '../../../shared/templates/subIndustry/subIndustry.model';
//Services
import { CompanyRiskService } from '../../companyRisk.service';

@Component({
    selector: 'mei-subindustry-risk-rating',
    templateUrl: '../../../shared/templates/subIndustry/subIndustry.component.html',
    styleUrls: ['../../../shared/templates/subIndustry/subIndustry.component.css']
})

export class SubIndustryMeiComponent extends SubIndustryModel {
    private sub: any;

    constructor(
        private route: ActivatedRoute,
        private riskService: CompanyRiskService,
        private http: Http) {
        super(http);
        this.primaryName = "Subindustry";
    }

    ngOnInit() {
        ;
        this.sub = this.route.params.subscribe(params => {
            this.load();
        });
    }

    private load(): void {
        this.loaded = new Promise<void>((resolve, reject) => {
            this.riskService.company.then(company => {
                this.riskService.mei.then(mei => {
                    this.currentCompany = company.id;
                    this.currentCompanyName = company.companyName;
                    this.companyClusterCode = mei.assessmentCode;
                    this.getMeiClusters().then(clusters => {
                        this.riskClusters = clusters;
                        this.maxExposure = 20;
                        var subIndustryPromise = this.riskService.getCompanyMeiSubIndustryBenchmark(company.id, mei.externalId);
                        var exposurePromise = this.riskService.getCompanyMeiSubIndustryExposure(company.id, mei.externalId);
                        Promise.all([subIndustryPromise, exposurePromise]).then(data => {
                            this.subIndustry = data[0] as any;
                            this.scatter = data[1] as any[];

                            //benchmark chart data
                            let benchmarkResult: any[] = []
                            let maxLimit: number = Math.min(this.subIndustry.chartData.length, this.riskClusters[this.riskClusters.length - 1].topInterval + 1);
                            let currentIndex: number = 0;
                            for (var i = 0; i < this.riskClusters.length; i++) {
                                benchmarkResult.push({
                                    score: this.subIndustry.chartData[i].xValue,
                                    primary: this.subIndustry.chartData[i].yValue,
                                    color: this.riskClusters[i].color,
                                    companyBullet: this.subIndustry.chartData[i].xValue == Math.floor(mei.riskRating)
                                });
                            }
                            this.benchmarkData = benchmarkResult;

                            //scatter chart data                        
                            let companyBullet: any;
                            let scatterResult: any[] = [];
                            this.scatter.forEach(subGroupMei => {
                                companyBullet = {
                                    management: subGroupMei.management,
                                    exposure: subGroupMei.exposure,
                                    color: this.clusterMetadata[subGroupMei.cluster].color
                                };

                                this.subIndustry.peersList.forEach(peer => {
                                    if (peer.companyId == subGroupMei.companyId) {
                                        companyBullet.peerPosition = peer.rank;
                                        if (subGroupMei.companyId == company.id) {
                                            companyBullet.currentX = companyBullet.exposure;
                                            companyBullet.currentY = companyBullet.management;
                                        }
                                        else {
                                            companyBullet.peerX = companyBullet.exposure;
                                            companyBullet.peerY = companyBullet.management;
                                        }
                                        return;
                                    }
                                });
                                scatterResult.push(companyBullet);
                            });
                            this.exposureData = scatterResult;
                            resolve();
                        });
                    });

                });
            });
        });
    }
}